package com.yonyou.gmmc.service.information.service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.marketing.common.util.CommonUtils;
import org.marketing.common.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yonyou.gmmc.service.information.dao.TmCarKnowledgesMapper;
import com.yonyou.gmmc.service.information.dao.TmExamineCarResultMapper;
import com.yonyou.gmmc.service.information.model.TmCarKnowledges;
import com.yonyou.gmmc.service.information.model.TmExamineCarResult;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;
@Service
@Transactional
public class CarKnowledgesService {
	private static Logger logger = LoggerFactory.getLogger(CarKnowledgesService.class);
	@Autowired
	private TmCarKnowledgesMapper tmCarKnowledgesMapper;
	
	@Autowired
	private TmExamineCarResultMapper tmExamineCarResultMapper;
	
	public PageInfo<TmCarKnowledges> findCarKnow(Map<String, Object> map) {
		logger.info("CarKnowledgesService.findProvince==>title:" + map.get("title"));
		// 查询用车知识
		Page<TmCarKnowledges> ss = PageHelper.startPage(Integer.parseInt(map.get("pageNum").toString()),
				Integer.parseInt(map.get("pageSize").toString()));
		logger.info("ss为————" + ss);
		Object title = map.get("title");
		Example example = new Example(TmCarKnowledges.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("status", 1);
		if(title!=null&&!"".equals(title)){
			criteria.andLike("title", "%"+title+"%");
		}
		example.setOrderByClause("PUBLISH_DATE desc");
		List<TmCarKnowledges> list = tmCarKnowledgesMapper.selectByExample(example);

		if (list != null && list.size() > 0) {
			logger.info("list为————" + list.toString());
			for (TmCarKnowledges carkonw : list) {
				String con = CommonUtils.getUrlToContent(carkonw.getContentUrl());
				carkonw.setContent(con);
			}
		} else {
			logger.info("list为————" + "");
		}
		PageInfo<TmCarKnowledges> page = new PageInfo<>(list);
		logger.info("page为————" + page);
		return page;
	}
	public List<TmCarKnowledges> findCarKnowDetail(Integer ckId) {
		logger.info("CarKnowledgesService.findCarKnowDetail==>ckId:" + ckId);
		// 查询用车知识
		Example example = new Example(TmCarKnowledges.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("status", 1);
		criteria.andEqualTo("ckId", ckId);
		List<TmCarKnowledges> list = new ArrayList<>();
		list = tmCarKnowledgesMapper.selectByExample(example);
		if (list != null && list.size() > 0) {
			logger.info("list为————" + list.toString());
			for (TmCarKnowledges carkonw : list) {
				String con = CommonUtils.getUrlToContent(carkonw.getContentUrl());
				carkonw.setContent(con);
			}
		} else {
			logger.info("list为————" + "");
		}
		return list;
	}
	
	
	/**
	 * 获取车系（爱车检查）
	 * @return
	 */
	public List<Map<String, Object>> acquireSeries() {
		logger.info("获取车系（爱车检查）==>start...");
		List<Map<String, Object>> result=tmExamineCarResultMapper.acquireSeries();
		logger.info("获取车系（爱车检查）==>end...");
		return result;
	}
	
	/**
	 * 获取车型（爱车检查）
	 * @param seriesId 
	 * @return
	 */
	public List<Map<String, Object>> acquireModel(Integer seriesId) {
		logger.info("获取车型（爱车检查）==>start...");
		List<Map<String, Object>> result=tmExamineCarResultMapper.acquireModel(seriesId);
		logger.info("获取车型（爱车检查）==>end...");
		return result;
	}
	
	
	/**
	 * 获取里程（爱车检查）
	 * @return
	 */
	public List<Map<String, Object>> acquireMileage() {
		logger.info("获取里程（爱车检查）==>start...");
		List<Map<String, Object>> result=tmExamineCarResultMapper.acquireMileage();
		logger.info("获取里程（爱车检查）==>end...");
		return result;
	}
	
	/**
	 * 获取结果（爱车检查）
	 * @param map
	 * @return
	 */
	public List<TmExamineCarResult> acquireResult(Integer modelId,String buyDate,Integer travelMileage) {
		logger.info("获取里程（爱车检查）==>start...");
		List<TmExamineCarResult> result = new ArrayList<>();
		if(modelId==null||buyDate==null||travelMileage==null||buyDate==""){
			logger.info("输入信息不完整！");
			TmExamineCarResult tmExamineCarResult=new TmExamineCarResult();
			tmExamineCarResult.setResultDescription("输入信息不完整！");
			result.add(tmExamineCarResult);
		}else{
			logger.info("日期>>>>>>>:"+buyDate);
			Date buyCarDate=DateUtil.dateFormat(buyDate, "yyyy-MM-dd HH:mm:ss");
			logger.info("购车日期>>>>>>>>："+buyCarDate);
			Date newDate=new Date();
			logger.info("当前日期："+newDate);
			if(newDate.before(buyCarDate)){
				logger.info("购车日期应在当前日期之前！");
				TmExamineCarResult tmExamineCarResult=new TmExamineCarResult();
				tmExamineCarResult.setResultDescription("购车日期应在当前日期之前！");
				result.add(tmExamineCarResult);
			}else{
				Calendar cbuyDate=Calendar.getInstance();
		        Calendar cnewDate=Calendar.getInstance();
		        
		        cbuyDate.setTime(buyCarDate);
		        cnewDate.setTime(newDate);
		        
		        int year =cnewDate.get(Calendar.YEAR)-cbuyDate.get(Calendar.YEAR);
		        
		        Integer differMonth=year*12+cnewDate.get(Calendar.MONTH)-cbuyDate.get(Calendar.MONTH);
		        logger.info("购车日期与当前日期的差："+differMonth);
				logger.info("查询所有使用月数");
				List<Integer> allMonthList = tmExamineCarResultMapper.queryAllMonth();
				
				Integer maxMonth=allMonthList.get(allMonthList.size()-1);
				logger.info("获取的最大月数："+maxMonth);
				Integer mileageResultId=tmExamineCarResultMapper.queryMileageResultId(modelId,travelMileage);
				logger.info("根据里程获取的mileageResultId"+mileageResultId);
				if(mileageResultId==null){
					TmExamineCarResult tmExamineCarResult=new TmExamineCarResult();
					tmExamineCarResult.setResultDescription("没有查询结果！");
					result.add(tmExamineCarResult);
				}else{
					if(differMonth>=maxMonth){
						logger.info("差月数大于最大月数");
						Integer monthResultId=tmExamineCarResultMapper.queryMonthResultId(modelId,maxMonth);
						logger.info("根据月份获取的monthResultId"+monthResultId);
						if(monthResultId==null){
							TmExamineCarResult tmExamineCarResult=new TmExamineCarResult();
							tmExamineCarResult.setResultDescription("没有查询结果！");
							result.add(tmExamineCarResult);
						}else{
							if(monthResultId>mileageResultId){
								result=tmExamineCarResultMapper.acquireResult(monthResultId);
							}else{
								result=tmExamineCarResultMapper.acquireResult(mileageResultId);
							}
						}
					}else{
						logger.info("差月数小于最大月数");
						Integer queryMonth = 0;
						for(Integer allMoth : allMonthList){
							if(differMonth<=allMoth){
								queryMonth=allMoth;
								break;
							}
						}
						logger.info("获取的月份queryMonth："+queryMonth);
						Integer monthResultId=tmExamineCarResultMapper.queryMonthResultId(modelId,queryMonth);
						logger.info("根据月份获取的monthResultId"+monthResultId);
						if(monthResultId==null){
							TmExamineCarResult tmExamineCarResult=new TmExamineCarResult();
							tmExamineCarResult.setResultDescription("没有检测结果！");
							result.add(tmExamineCarResult);
						}else{
							if(monthResultId>mileageResultId){
								result=tmExamineCarResultMapper.acquireResult(monthResultId);
							}else{
								result=tmExamineCarResultMapper.acquireResult(mileageResultId);
							}
						}
					}
				}
			}
		}
		logger.info("获取里程（爱车检查）==>end...");
		return result;
	}
	
	
}
